package com.daydayup.learn;

public class TwoNumSum {
    public static void main(String[] args) {
//        int[] numbers = new int[]{2,7,11,15};
//        int target = 18;
//        for (int n : twoSum(numbers, target)) {
//            System.out.println(n);
//        }
        int[] nums = new int[]{-1,0,3,5,9,12};
        int target = 12;
        System.out.println(binarySearch(nums, target));
    }

    static int binarySearch(int[] nums, int target){
        int right = nums.length-1;
        int left = 0;
        while (left <= right) {
            int middle = left + (right - left) / 2;
            if (nums[middle] > target) {
                right = middle - 1;
            } else if (nums[middle] < target) {
                left = middle + 1;
            } else {
                return middle;
            }
        }
        return -1;
    }

    static int[] twoSum(int[] nums, int target) {
        int left = 0;
        int right = nums.length-1;
        int[] result = new int[2];

        while (left < right) {

            if (nums[left] + nums[right] == target) {
                result[0] = left+1;
                result[1] = right+1;
                return result;
            } else if (nums[left] + nums[right] > target) {
                right--;
            } else {
                left++;
            }
        }
        result[0] = -1;
        result[1] = -1;
        return result;
    }
}
